﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.VehicleReport.BLL;
using Yipusoft.VehicleReport.Model;
using Yipusoft.Vehicle.Model;
using Yipusoft.Vehicle.BLL;
using Yipusoft.BasicBase.Model;
using Yipusoft.BasicBase.BLL;
using Yipusoft.Common.BLL;

public partial class Police_SecurityEvaluate_MendZhongdstat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }
    string GetDeviceCountUnit(MendDeviceInfo menddevice)
    {
        string strCount = menddevice.Count.ToString();
        DeviceInfo device = Device.GetDevice(menddevice.DeviceId);
        if (device != null)
        {
            Yipusoft.Common.Model.CodeItemInfo iteminfo = CodeItem.GetCodeItem(device.UnitName);
            string UnitName = "";

            if (iteminfo != null)
            {
                UnitName = iteminfo.ItemName;

            }
            string[] strCounts = strCount.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);

            if (strCounts.Count() > 1)
            {
                if (Convert.ToInt32(strCounts[1]) > 0)
                {
                    strCount = strCount + UnitName;
                }
                else
                {
                    strCount = strCounts[0] + UnitName;
                }
            }
            else
            {
                strCount = strCounts[0] + UnitName;
            }

            return strCount;
        }
        return strCount;
    }

    void BindData()
    {
        DateTime date = DateTime.Now.Date;
        string DepartmentId = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        lbldate.Text = date.ToString("yyyy年MM月dd日") + Department.GetDepartment(DepartmentId).FullName + "车辆装备检查情况报表";
        if (DepartmentId.Substring(4, 1) == "3")
        {
            table.Rows.Clear();
            //头
            HtmlTableRow tr = new HtmlTableRow();
            HtmlTableCell td = new HtmlTableCell();


            td = new HtmlTableCell();
            td.InnerText = "车辆";
            td.RowSpan = 2;
            td.Width = "18%";
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");
            tr.Cells.Add(td);

            td = new HtmlTableCell();
            td.RowSpan = 2;
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");
            td.InnerText = "损坏情况";
            tr.Cells.Add(td);

            td = new HtmlTableCell();
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");
            td.InnerText = "维修完成确认";

            td.ColSpan = 3;
            tr.Cells.Add(td);

            table.Rows.Add(tr);


            tr = new HtmlTableRow();
            td = new HtmlTableCell();
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");
            td.InnerText = "支队";

            td.Width = "15%";
            tr.Cells.Add(td);

            td = new HtmlTableCell();
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");
            td.InnerText = "大队";
            td.Width = "15%";
            tr.Cells.Add(td);
            table.Rows.Add(tr);

            td = new HtmlTableCell();
            td.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");
            td.InnerText = "中队";
            td.Width = "15%";
            tr.Cells.Add(td);
            table.Rows.Add(tr);


            //车辆损坏列表
            List<Guid> vids = MendRecord.GetMendVehiclesByDepartmentid(DepartmentId);
            foreach (Guid vid in vids)
            {
                HtmlTableRow row = new HtmlTableRow();

                string recordWhere = "";
                string vehileName = null;
                VehicleInfo vehile = Vehicle.GetDetail(vid);
                if (vehile != null)
                {
                    vehileName = vehile.VehicleName + "<br/>【" + vehile.NumberPlate + "】";
                }
                else
                {
                    recordWhere = " DepartmentId='" + DepartmentId + "' and VehicleID='" + vid + "' and ZhongdOk='' ";
                    MendRecordInfo recordVname = MendRecord.GetList(recordWhere).FirstOrDefault();
                    if (recordVname != null)
                    {
                        vehileName = recordVname.VehicleName + "<br/>此车辆已删除！";
                    }
                    else
                    {

                        vehileName = "此车辆已删除！";

                    }
                }

                HtmlTableCell VNameCell = new HtmlTableCell();
                VNameCell.InnerHtml = vehileName;
                VNameCell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: left;");

                row.Cells.Add(VNameCell);
                recordWhere = " DepartmentId='" + DepartmentId + "' and VehicleID='" + vid + "' and ZhongdOk='' ";
                MendRecordInfo record = MendRecord.GetRecordByDepartmentIdAndVid(DepartmentId, vid);

                int ros = 0;

                //车辆
                if (record != null)
                {
                    ros++;
                    //情况
                    HtmlTableCell cell = new HtmlTableCell();
                    if (record.MendTime.Date < date)
                    {
                        TimeSpan timeLong = date - record.MendTime.Date;
                        int DayCount = timeLong.Days;
                        cell.InnerHtml = record.Remark + "<br/><div style='color:Red;'>上报时间为：" + record.MendTime.ToString("yyyy-MM-dd") + "，距今已" + DayCount + "天。</div>";
                    }
                    else
                    {
                        cell.InnerHtml = record.Remark;
                    }
                    cell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px;   border-width: 1px; border-style:solid;text-align: left;");

                    row.Cells.Add(cell);
                    //支队
                    cell = new HtmlTableCell();
                    cell.InnerHtml = record.ZhiDOK;
                    cell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");

                    row.Cells.Add(cell);

                    //大队
                    cell = new HtmlTableCell();
                    cell.InnerHtml = record.DZDOk;
                    cell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");

                    row.Cells.Add(cell);

                    //中队
                    cell = new HtmlTableCell();

                    Button btnOk = new Button();
                    btnOk.Text = "确认";
                    btnOk.CommandName = "OK";
                    btnOk.OnClientClick = "return confirm('是否真的确认维修完好?')";
                    btnOk.CommandArgument = record.ID.ToString();
                    btnOk.CssClass = "button_default";

                    btnOk.Command += new CommandEventHandler(btnOk_Command);

                    cell.Controls.Add(btnOk);


                    cell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");

                    row.Cells.Add(cell);

                    table.Rows.Add(row);
                }

                //随车器材
                string DeviceWhere = " DepartmentId='" + DepartmentId + "' and Vid='" + vid + "' and ZhongdOk='' ";

                List<MendDeviceInfo> Devices = MendDevice.GetList(DeviceWhere);
                foreach (MendDeviceInfo device in Devices)
                {
                    if (ros != 0)
                    {
                        row = new HtmlTableRow();
                    }
                    ros++;
                    //情况
                    HtmlTableCell cell = new HtmlTableCell();
                    cell.InnerHtml = device.DeviceName;

                    cell.InnerHtml += "&nbsp;&nbsp;&nbsp;&nbsp;" + GetDeviceCountUnit(device);

                    cell.InnerHtml += "&nbsp;&nbsp;&nbsp;&nbsp;" + device.Remark;
                    if (device.DealNotes != "")
                    {
                        cell.InnerHtml += "<br/>" + device.DealNotes;
                    }
                    if (device.MendTime.Date < date)
                    {
                        TimeSpan timeLong = date - device.MendTime.Date;
                        int DayCount = timeLong.Days;
                        cell.InnerHtml += "<br/><div style='color:Red;'>上报时间为：" + device.MendTime.ToString("yyyy-MM-dd") + "，距今已" + DayCount + "天。</div>";
                    }

                    cell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; border-width: 1px; border-style:solid;text-align: left;");

                    row.Cells.Add(cell);
                    //支队
                    cell = new HtmlTableCell();
                    cell.InnerHtml = device.ZhidOk;
                    cell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");

                    row.Cells.Add(cell);

                    //大队
                    cell = new HtmlTableCell();
                    cell.InnerHtml = device.DadOk;
                    cell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");

                    row.Cells.Add(cell);

                    //中队
                    cell = new HtmlTableCell();
                    Button btnOk2 = new Button();
                    btnOk2.Text = "确认";
                    btnOk2.CommandName = "OK";
                    btnOk2.OnClientClick = "return confirm('是否真的确认维修完好?')";
                    btnOk2.CommandArgument = device.ID.ToString();
                    btnOk2.CssClass = "button_default";

                    btnOk2.Command += new CommandEventHandler(btnOk2_Command);

                    cell.Controls.Add(btnOk2);
                    cell.Attributes.Add("style", "border-color:#888888;border-collapse:  collapse; padding: 4px; white-space:nowrap;  border-width: 1px; border-style:solid;text-align: center;");

                    row.Cells.Add(cell);

                    table.Rows.Add(row);
                }
                VNameCell.RowSpan = ros;
            }
        }
        else
        {

            return;
        }
    }
    //车辆中队确定
    void btnOk_Command(object sender, CommandEventArgs e)
    {
        Guid id = new Guid(e.CommandArgument.ToString());
        MendRecordInfo Reco = MendRecord.GetDetail(id);
        Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        string departmentName = Yipusoft.Account.BLL.Department.GetDepartment(user.DepartmentID).ShortName;
        Reco.ZhongdOk += Reco.ZhongdOk == "" ? "" : "<br/><br/>";
        Reco.ZhongdOk += departmentName + "<br/>" + user.UserName + "<br/>" + DateTime.Now.ToShortDateString();
        Reco.FinishTime = DateTime.Now;
        MendRecord.Update(Reco);
        Response.Redirect("MendZhongdstat.aspx");
    }
    //
    void btnOk2_Command(object sender, CommandEventArgs e)
    {
        Guid id = new Guid(e.CommandArgument.ToString());
        MendDeviceInfo device = MendDevice.GetDetail(id);
        Yipusoft.Account.Model.UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        string departmentName = Yipusoft.Account.BLL.Department.GetDepartment(user.DepartmentID).ShortName;
        device.ZhongdOk += device.ZhongdOk == "" ? "" : "<br/><br/>";
        device.ZhongdOk += departmentName + "<br/>" + user.UserName + "<br/>" + DateTime.Now.ToShortDateString();
        device.FinishTime = DateTime.Now;
        MendDevice.Update(device);
        Response.Redirect("MendZhongdstat.aspx");
    }
}
